package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.common.Constants;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Manager;
import com.czd.support.model.News;
import com.czd.support.service.DataDictionaryService;
import com.czd.support.service.NewsService;

/**
 * 新闻控制类
 * @author Jun.Yang
 *
 */
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {
	@Autowired
	private NewsService newsService;
	@Autowired
	private DataDictionaryService dataDictionaryService;
	
	/**
	 * 新闻类别
	 * @return
	 */
	@ModelAttribute("newsTypeList")
	protected List<DataDictionary> initNewsType() {
		return dataDictionaryService.queryListByCode(Constants.NEWS_CODE);
	}
	
	/**
	 * 新闻信息列表 
	 * @param news
	 * @param request
	 * @param model
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = {"/", "", "list"}, method = RequestMethod.GET)
	public String list(News news, HttpServletRequest request, Model model, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		news.setPageSize(Configuration.getInstance().getDefault_page_size());
		news.setPageNo(pageNo);
		Paginate paginate = newsService.queryForPage(news);
		model.addAttribute("paginate", paginate);
		return "news/list";
	}
	
	/**
	 * 跳转到新闻信息录入页面
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return "news/add";
	}
	
	/**
	 * 新闻信息录入
	 * @param request
	 * @param news
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, News news, Model model) {
		Manager manager = this.getSesionUser(request);
		news.setCreateUser(manager.getUserCode());
		if(newsService.insert(news)) {
			return "redirect:/news/"+news.getId()+"/details";
		}
		
		return error(model, "操作失败", "录入新闻信息失败");
	}
	
	/**
	 * 跳转到新闻信息编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		News news = newsService.findById(id);
		System.out.println(news);
		if(news!=null) {
			model.addAttribute("news", news);
			return "news/edit";
		}
		
		return error(model, "新闻信息不存在", "新闻信息不存在");
	}
	
	/**
	 * 更新新闻信息
	 * @param request
	 * @param news
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST) 
	public String edit(HttpServletRequest request, News news, Model model){
		Manager manager = this.getSesionUser(request);
		news.setUpdateUser(manager.getUserCode());
		if(newsService.update(news)) {
			return "redirect:/news/"+news.getId()+"/details";
		}
		
		return  error(model, "操作失败", "更新新闻信息失败");
	}

	/**
	 * 删除新闻信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable(value="id") int id, Model model) {
		if(newsService.delete(id)) {
			return "redirect:/news/list";
		}
		
		return error(model, "操作失败", "删除新闻信息失败");
	}
	
	/**
	 * 新闻信息详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		News news = newsService.findById(id);
		if(news!=null) {
			model.addAttribute("news", news);
			return "news/details";
		}
		
		return error(model, "新闻信息不存在", "新闻信息不存在");
	}
 }
